<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!--<link rel="stylesheet" type="text/css" href="common/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="common/bootstrap/css/bootstrap.css" media="all">
    <link rel="stylesheet" type="text/css" href="common/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="css/personal.css" media="all">-->

    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/cba/style.css/}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/cba/my.css/}" media="all">
</head>
<body class="childrenBody">
<form class="layui-form html_save" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label">登陆名</label>
        <div class="layui-input-block">
            <input class="layui-input" name="username" lay-verify="username" placeholder="请输入名称" type="text">
            <input class="layui-input" name="id" id="id" type="hidden">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input class="layui-input" name="name" lay-verify="required" placeholder="请输入姓名" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <select name="roleid" lay-filter="role" lay-verify="required" id="role">
                <option value="">请选择</option>
                <option th:each="entry : ${roleMap}" th:value="${entry.value}" th:text="${entry.key}"></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item" lay-filter="company">
        <label class="layui-form-label">公司</label>
        <div class="layui-input-block">
            <select name="companyid" id="company">
                <option value="">请选择</option>
                <option th:each="entry : ${companyMap}" th:value="${entry.value}" th:text="${entry.key}"></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" checked lay-skin="switch" lay-text="开启|暂停" id="status">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input class="layui-input" name="mobile" placeholder="请输入手机号" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input class="layui-input" name="email" placeholder="请输入邮箱" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">推荐人</label>
        <div class="layui-input-block">
            <input class="layui-input" name="referee" placeholder="请输入推荐人" type="text">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入备注" class="layui-textarea" name="memo"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveContainer">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script th:src="@{/js/layui/layui.js}"></script>
<script th:src="@{/js/cba/login-timeout.js}"></script>
<!--<script type="text/javascript" src="js/newsadd.js"></script>-->

<script type="text/javascript" th:inline="javascript">
    layui.use(['table', 'form', "layer"], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , $ = layui.jquery;

        var user = [[${user}]];
        if (user != null) {
            form.val("form", {
                "username": user.username // "name": "value"
                , "name": user.name
                , "phone": user.phone
                , "email": user.email
                , "memo": user.memo
                , "referee": user.referee
                , "id": user.id
            });

            $("#role").val(user.roleid);
            $("#company").val(user.companyid);
            form.render("select", null); //刷新select选择框渲染

            if (user.status == 1) {
                $("#status").removeAttr("checked");
            }
            form.render("checkbox"); //刷新select选择框渲染

        }

        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (!new RegExp("^[A-Za-z0-9]+$").test(value)) {
                    return '用户名只能为数字或字母';
                }
            }
        });

        //监听提交
        form.on('submit(saveContainer)', function (data) {

            data.field.status = data.field.status == "on" ? 0 : 1;
            $.ajax({
                url: [[${basePath}]] + "/user/save", data: data.field, sync: "post", success: function (data) {
                    console.log(data);
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.href = [[${basePath}]] + "/user/index";
                            parent.layer.close(index);
                        }, 1500);
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });

            return false;
        });

    });
</script>
</body>
</html>